{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b5aa1186-db91-474a-86e8-4509394382a9",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 38.MAX30102脉搏血氧及心率传感器实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：38.MAX30102脉搏血氧及心率传感器实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：MAX30102脉搏血氧及心率传感器实验\n",
    "MAX30102脉搏血氧及心率传感器实验，通过第三方自定义的heartrate_monitor库操作该传感器模块，该传感器模块采用的是I2C的通讯方式，可以直接读取脉搏及血氧含量！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "445be920-d473-45b8-8aa6-e4b55af36b2e",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d9d214c4-40b8-454d-b76f-33676b68fb1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from heartrate_monitor import HeartRateMonitor\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbee25d9-eeff-4358-8858-030d6724a8f1",
   "metadata": {},
   "source": [
    "## 2.初始化脉搏血氧仪心率传感器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8061dfaa-9d83-4472-90c2-1fc60451907a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Makerobo Max30102 Sensor starting...\n"
     ]
    }
   ],
   "source": [
    "# 打印出一条信息说明传感器开始启动\n",
    "print('Makerobo Max30102 Sensor starting...')\n",
    "#设置持续读取时间\n",
    "durationtime = 15"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49b9235a-0ecd-4e59-bae2-dbb73493c916",
   "metadata": {},
   "source": [
    "## 3.设置传感器输出信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cdf51c59-79a3-4e8b-b4d5-b8dbc1ea1fdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化hearttratemonitor对象\n",
    "# 将print_raw设置为False以避免打印原始数据\n",
    "# 设置print_result为True，打印计算结果\n",
    "makerobo_hrm = HeartRateMonitor(print_raw=False, print_result=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39cd4d39-7284-48c4-920c-59969df8900a",
   "metadata": {},
   "source": [
    "## 5.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a89b2dfd-31a9-4982-9059-42b33115b115",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BPM: 214.0, SpO2: -999\n",
      "BPM: 190.0, SpO2: 98.116266\n",
      "BPM: 176.66666666666666, SpO2: 92.41010399999999\n",
      "BPM: 170.0, SpO2: 92.41010399999999\n",
      "BPM: 154.0, SpO2: 92.41010399999999\n",
      "BPM: 146.5, SpO2: 94.129194\n",
      "BPM: 143.0, SpO2: 94.129194\n",
      "BPM: 139.5, SpO2: 92.41010399999999\n",
      "BPM: 136.0, SpO2: 92.41010399999999\n",
      "BPM: 136.0, SpO2: 92.41010399999999\n",
      "BPM: 133.25, SpO2: 86.886066\n",
      "BPM: 130.5, SpO2: 86.886066\n",
      "BPM: 134.0, SpO2: 92.41010399999999\n",
      "BPM: 137.5, SpO2: 92.41010399999999\n",
      "BPM: 140.25, SpO2: 82.72964999999999\n",
      "BPM: 143.0, SpO2: 82.72964999999999\n",
      "BPM: 139.5, SpO2: 82.72964999999999\n",
      "BPM: 136.0, SpO2: 82.72964999999999\n",
      "BPM: 133.25, SpO2: 82.72964999999999\n",
      "BPM: 130.5, SpO2: 82.72964999999999\n",
      "BPM: 127.75, SpO2: 82.72964999999999\n",
      "BPM: 125.0, SpO2: 82.72964999999999\n",
      "BPM: 122.5, SpO2: 82.72964999999999\n",
      "BPM: 120.0, SpO2: 82.72964999999999\n",
      "BPM: 117.5, SpO2: -999\n",
      "BPM: 115.0, SpO2: -999\n",
      "BPM: 115.0, SpO2: -999\n",
      "BPM: 113.0, SpO2: -999\n",
      "BPM: 111.0, SpO2: -999\n",
      "BPM: 109.0, SpO2: -999\n",
      "BPM: 107.0, SpO2: -999\n",
      "BPM: 107.0, SpO2: -999\n",
      "BPM: 111.5, SpO2: -999\n",
      "BPM: 116.0, SpO2: -999\n",
      "BPM: 120.5, SpO2: -999\n",
      "BPM: 125.0, SpO2: -999\n",
      "BPM: 122.5, SpO2: -999\n",
      "BPM: 113.25, SpO2: 97.335786\n",
      "BPM: 104.0, SpO2: 97.335786\n",
      "BPM: 96.0, SpO2: 95.622984\n",
      "BPM: 90.5, SpO2: 95.622984\n",
      "BPM: 91.75, SpO2: 95.622984\n",
      "BPM: 93.0, SpO2: 95.622984\n",
      "BPM: 93.0, SpO2: 91.65938399999999\n",
      "BPM: 90.5, SpO2: 85.395\n",
      "BPM: 88.0, SpO2: 85.395\n",
      "BPM: 85.5, SpO2: 85.395\n",
      "BPM: 83.0, SpO2: 93.468594\n",
      "BPM: 83.0, SpO2: 93.468594\n",
      "BPM: 83.0, SpO2: 93.468594\n",
      "BPM: 89.0, SpO2: 93.468594\n",
      "BPM: 95.0, SpO2: 93.468594\n",
      "BPM: 101.0, SpO2: 93.468594\n",
      "BPM: 107.0, SpO2: 93.468594\n",
      "BPM: 121.75, SpO2: 93.468594\n",
      "BPM: 136.5, SpO2: 93.468594\n",
      "BPM: 151.25, SpO2: 93.468594\n",
      "BPM: 166.0, SpO2: 93.468594\n",
      "BPM: 166.0, SpO2: 93.468594\n",
      "BPM: 158.5, SpO2: 93.468594\n",
      "BPM: 151.0, SpO2: 93.468594\n",
      "BPM: 143.5, SpO2: 93.468594\n",
      "BPM: 133.25, SpO2: 93.468594\n",
      "BPM: 130.5, SpO2: 93.468594\n",
      "BPM: 127.75, SpO2: 93.468594\n",
      "BPM: 127.75, SpO2: 93.468594\n",
      "BPM: 130.5, SpO2: 93.468594\n",
      "BPM: 133.25, SpO2: 93.468594\n",
      "BPM: 143.5, SpO2: 93.468594\n",
      "BPM: 147.0, SpO2: 93.468594\n",
      "BPM: 147.0, SpO2: 93.468594\n",
      "BPM: 147.0, SpO2: 93.468594\n",
      "BPM: 139.5, SpO2: 93.468594\n",
      "BPM: 139.5, SpO2: 93.468594\n",
      "BPM: 143.0, SpO2: 93.468594\n",
      "BPM: 146.5, SpO2: 93.468594\n",
      "BPM: 150.0, SpO2: 93.468594\n",
      "BPM: 154.0, SpO2: 93.468594\n",
      "BPM: 163.25, SpO2: 98.33924999999999\n",
      "BPM: 167.25, SpO2: 98.33924999999999\n",
      "BPM: 171.25, SpO2: 98.33924999999999\n",
      "BPM: 171.25, SpO2: 98.33924999999999\n",
      "BPM: 166.0, SpO2: 98.33924999999999\n",
      "BPM: 166.0, SpO2: 98.33924999999999\n",
      "BPM: 171.25, SpO2: 98.33924999999999\n",
      "BPM: 176.5, SpO2: 98.33924999999999\n",
      "BPM: 181.75, SpO2: 98.33924999999999\n",
      "BPM: 187.0, SpO2: 98.33924999999999\n",
      "BPM: 187.0, SpO2: 35.346666\n",
      "BPM: 187.0, SpO2: 35.346666\n",
      "BPM: 181.75, SpO2: 99.674706\n",
      "BPM: 176.5, SpO2: 99.674706\n",
      "BPM: 171.25, SpO2: 99.674706\n",
      "BPM: 171.25, SpO2: 99.519096\n",
      "BPM: 176.5, SpO2: 99.519096\n",
      "BPM: 176.5, SpO2: 98.33924999999999\n",
      "BPM: 176.5, SpO2: 98.33924999999999\n",
      "BPM: 171.25, SpO2: 98.33924999999999\n",
      "BPM: 166.0, SpO2: 98.33924999999999\n",
      "BPM: 166.0, SpO2: 98.288856\n",
      "BPM: 166.0, SpO2: 98.288856\n",
      "BPM: 171.25, SpO2: 98.452434\n",
      "BPM: 176.5, SpO2: 98.452434\n",
      "BPM: 181.75, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 98.452434\n",
      "BPM: 187.0, SpO2: 99.727416\n",
      "BPM: 187.0, SpO2: 98.752554\n",
      "BPM: 181.75, SpO2: 99.727416\n",
      "BPM: 176.5, SpO2: 99.848136\n",
      "BPM: 171.25, SpO2: 95.34225\n",
      "BPM: 171.25, SpO2: 98.452434\n",
      "BPM: 176.5, SpO2: 98.452434\n",
      "BPM: 172.5, SpO2: 99.24465\n",
      "BPM: 168.5, SpO2: 99.24465\n",
      "BPM: 159.25, SpO2: 99.24465\n",
      "BPM: 159.25, SpO2: 99.758856\n",
      "BPM: 168.5, SpO2: 99.758856\n",
      "BPM: 172.5, SpO2: 98.794776\n",
      "BPM: 176.5, SpO2: 98.794776\n",
      "BPM: 171.25, SpO2: 98.794776\n",
      "BPM: 166.0, SpO2: 99.758856\n",
      "BPM: 162.0, SpO2: 99.0534\n",
      "BPM: 158.0, SpO2: 99.0534\n",
      "BPM: 154.0, SpO2: 99.0534\n",
      "BPM: 150.0, SpO2: 99.0534\n",
      "BPM: 150.0, SpO2: 98.928594\n",
      "BPM: 146.5, SpO2: 98.500104\n",
      "BPM: 143.0, SpO2: 98.500104\n",
      "BPM: 139.5, SpO2: 98.928594\n",
      "BPM: 136.0, SpO2: 98.928594\n",
      "BPM: 133.25, SpO2: 99.758856\n",
      "BPM: 126.0, SpO2: 99.54225\n",
      "BPM: 120.75, SpO2: 99.54225\n",
      "BPM: 115.5, SpO2: 99.54225\n",
      "BPM: 115.5, SpO2: 99.59255399999999\n",
      "BPM: 120.0, SpO2: 99.59255399999999\n",
      "BPM: 122.5, SpO2: 99.59255399999999\n",
      "BPM: 131.25, SpO2: 99.727416\n",
      "BPM: 137.5, SpO2: 99.727416\n",
      "BPM: 143.75, SpO2: 99.727416\n",
      "BPM: 146.5, SpO2: 99.674706\n",
      "BPM: 143.0, SpO2: 99.674706\n",
      "BPM: 139.5, SpO2: 99.674706\n",
      "BPM: 136.0, SpO2: 99.674706\n",
      "BPM: 148.75, SpO2: 99.84405\n",
      "BPM: 161.5, SpO2: 99.84405\n",
      "BPM: 174.25, SpO2: 99.84405\n",
      "BPM: 181.75, SpO2: 99.84405\n",
      "BPM: 176.5, SpO2: 99.84405\n",
      "BPM: 171.25, SpO2: 99.84405\n",
      "BPM: 171.25, SpO2: 99.84405\n",
      "BPM: 176.5, SpO2: 99.84405\n",
      "BPM: 181.75, SpO2: 99.84405\n",
      "BPM: 187.0, SpO2: 99.84405\n",
      "BPM: 187.0, SpO2: 99.84405\n",
      "BPM: 181.75, SpO2: 99.674706\n",
      "BPM: 176.5, SpO2: 99.674706\n",
      "BPM: 171.25, SpO2: 99.84405\n",
      "BPM: 166.0, SpO2: 99.84405\n",
      "BPM: 166.0, SpO2: 99.84405\n",
      "BPM: 171.25, SpO2: 99.84405\n",
      "BPM: 167.25, SpO2: 99.84405\n",
      "BPM: 163.25, SpO2: 99.84405\n",
      "BPM: 168.5, SpO2: 99.59255399999999\n",
      "BPM: 163.25, SpO2: 99.824664\n",
      "BPM: 167.25, SpO2: 99.824664\n",
      "BPM: 171.25, SpO2: 99.824664\n",
      "BPM: 171.25, SpO2: 99.59255399999999\n",
      "BPM: 176.5, SpO2: 99.59255399999999\n",
      "BPM: 181.75, SpO2: 99.59255399999999\n",
      "BPM: 187.0, SpO2: 99.59255399999999\n",
      "BPM: 187.0, SpO2: 99.59255399999999\n",
      "BPM: 187.0, SpO2: 99.59255399999999\n",
      "BPM: 187.0, SpO2: 99.59255399999999\n",
      "BPM: 187.0, SpO2: 99.59255399999999\n",
      "BPM: 187.0, SpO2: 98.607\n",
      "BPM: 187.0, SpO2: 98.607\n",
      "BPM: 181.75, SpO2: -999\n",
      "BPM: 176.5, SpO2: -999\n",
      "BPM: 171.25, SpO2: -999\n",
      "BPM: 162.0, SpO2: 89.62487399999999\n",
      "BPM: 158.0, SpO2: 89.62487399999999\n",
      "BPM: 147.75, SpO2: 76.72305\n",
      "BPM: 147.75, SpO2: -999\n",
      "BPM: 151.75, SpO2: -999\n",
      "BPM: 155.75, SpO2: -999\n",
      "BPM: 162.0, SpO2: 92.03925\n",
      "BPM: 158.0, SpO2: 92.03925\n",
      "BPM: 154.0, SpO2: 92.03925\n",
      "BPM: 146.5, SpO2: 41.654226\n",
      "BPM: 150.5, SpO2: 92.03925\n",
      "BPM: 154.5, SpO2: 92.03925\n",
      "BPM: 158.5, SpO2: 92.03925\n",
      "BPM: 166.0, SpO2: 92.03925\n",
      "BPM: 166.0, SpO2: 92.03925\n",
      "BPM: 162.0, SpO2: 92.03925\n",
      "BPM: 158.0, SpO2: 92.03925\n",
      "BPM: 154.0, SpO2: 92.03925\n",
      "BPM: 150.0, SpO2: 92.03925\n",
      "BPM: 146.5, SpO2: 76.72305\n",
      "BPM: 143.0, SpO2: 76.72305\n",
      "BPM: 139.5, SpO2: 76.72305\n",
      "BPM: 136.0, SpO2: 76.72305\n",
      "BPM: 136.0, SpO2: 84.879954\n",
      "BPM: 128.75, SpO2: 69.81524999999999\n",
      "BPM: 114.25, SpO2: 94.753746\n",
      "BPM: 99.75, SpO2: 93.8034\n",
      "BPM: 83.5, SpO2: 71.26890599999999\n",
      "BPM: 68.75, SpO2: 71.26890599999999\n",
      "BPM: 61.25, SpO2: 71.26890599999999\n",
      "BPM: 53.0, SpO2: 81.02229599999998\n",
      "BPM: 46.5, SpO2: 81.02229599999998\n",
      "BPM: 45.75, SpO2: 81.02229599999998\n",
      "BPM: 50.0, SpO2: 71.26890599999999\n",
      "BPM: 55.0, SpO2: 71.26890599999999\n",
      "BPM: 60.0, SpO2: 71.26890599999999\n",
      "BPM: 65.0, SpO2: 71.26890599999999\n",
      "BPM: 65.75, SpO2: 54.207624\n",
      "BPM: 66.5, SpO2: 54.207624\n",
      "BPM: 73.5, SpO2: 71.26890599999999\n",
      "BPM: 80.5, SpO2: 71.26890599999999\n",
      "BPM: 86.75, SpO2: 71.26890599999999\n",
      "BPM: 132.25, SpO2: 54.207624\n",
      "BPM: 155.75, SpO2: 54.207624\n",
      "BPM: 179.25, SpO2: 54.207624\n",
      "BPM: 202.75, SpO2: 54.207624\n",
      "BPM: 193.75, SpO2: 54.207624\n",
      "BPM: 209.5, SpO2: 54.207624\n",
      "BPM: 225.25, SpO2: 54.207624\n",
      "BPM: 241.0, SpO2: 54.207624\n",
      "BPM: 241.0, SpO2: 54.207624\n",
      "BPM: 232.0, SpO2: 54.207624\n",
      "BPM: 223.0, SpO2: 54.207624\n",
      "BPM: 207.25, SpO2: 99.016626\n",
      "BPM: 200.5, SpO2: 99.016626\n",
      "BPM: 188.5, SpO2: 99.016626\n",
      "BPM: 176.5, SpO2: 99.016626\n",
      "BPM: 171.25, SpO2: 99.016626\n",
      "BPM: 178.0, SpO2: 91.270506\n",
      "BPM: 190.0, SpO2: 91.270506\n",
      "BPM: 195.25, SpO2: 97.335786\n",
      "BPM: 200.5, SpO2: 97.335786\n",
      "BPM: 193.75, SpO2: 97.335786\n",
      "BPM: 193.75, SpO2: 91.270506\n",
      "BPM: 200.5, SpO2: 91.270506\n",
      "BPM: 207.25, SpO2: 99.796266\n",
      "BPM: 214.0, SpO2: 99.796266\n",
      "BPM: 214.0, SpO2: 99.796266\n",
      "BPM: 207.25, SpO2: 99.796266\n",
      "BPM: 200.5, SpO2: 99.796266\n",
      "BPM: 193.75, SpO2: 99.796266\n",
      "BPM: 187.0, SpO2: 99.796266\n",
      "Makerobo Max30102 Sensor stopped!\n"
     ]
    }
   ],
   "source": [
    "# 程序入口\n",
    "if __name__ == '__main__':\n",
    "    # 开始读取传感器\n",
    "    makerobo_hrm.start_sensor()\n",
    "    try:\n",
    "        time.sleep(durationtime)  # 持续时间\n",
    "    except KeyboardInterrupt:\n",
    "        print('keyboard interrupt detected, exiting...')\n",
    "    \n",
    "    # 在持续时间过去后停止传感器\n",
    "    makerobo_hrm.stop_sensor()\n",
    "\n",
    "    # 打印一条消息，表明传感器已停止\n",
    "    print('Makerobo Max30102 Sensor stopped!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f9c09bc-d6e2-430c-badb-a37cd1f9a440",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
